package com.xinghou.platform.lucene.main.impl;

import com.xinghou.platform.dao.main.FavoriteDAO;
import com.xinghou.platform.dao.main.SubjectDAO;
import com.xinghou.platform.entity.resp.SubjectResp;
import com.xinghou.platform.model.main.FavoriteSubject;
import com.xinghou.platform.server.api.bussiness.FavoriteSubjectService;
import com.xinghou.platform.server.api.bussiness.SubjectService;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qingshengzheng on 16/6/23.
 */
@Service("favoriteSubjectService")
public class FavoriteSubjectServiceImpl implements FavoriteSubjectService {
    @Resource
    private SubjectDAO subjectDAO;
    @Resource
    private FavoriteDAO favoriteDAO;
    @Resource
    private SubjectService subjectService;
    @Override
    public void add(FavoriteSubject favoriteSubject) {
        int n=favoriteDAO.hasFavoritySubject(favoriteSubject.getUid(), favoriteSubject.getSubjectid());
        if(n>0)
            throw new ServiceException(DBExceptionEnums.HAS_FAVORITE_SUBJECT);
        favoriteSubject.createtime=System.currentTimeMillis();
        favoriteDAO.addFavoriteSubject(favoriteSubject);
    }
    @Override
    public List<SubjectResp> getFavoriteSubjectList(Long uid, Integer pageno, Integer pagesize) {
        List<SubjectResp> subjectResps=new ArrayList<SubjectResp>();
        Integer startno=(pageno-1)*pagesize;
        List<Long> subjectids = favoriteDAO.getFavoriteSubjectList(uid, startno, pagesize);
        for(int i=0;i<subjectids.size();i++)
        {
            SubjectResp subject=subjectService.subjectDetailInfo(uid,subjectids.get(i));
            if(subject!=null)
            subjectResps.add(subject);
        }
        return subjectResps;
    }

    @Override
    public void del(Long uid,Long id) {
        favoriteDAO.delFavoriteSubject(uid,id);
    }
}
